﻿/*
* Created by SharpDevelop.
* User: hang
* Date: 2010-1-5
* Time: 19:50
* 
* To change this template use Tools | Options | Coding | Edit Standard Headers.
*/

using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace PingBall
{
    /// <summary>
    /// Description of ProcessMemory.
    /// </summary>
    public class ProcessMemory
    {
        //进程句柄
        IntPtr ProcessHandle;
        //进程ID
        System.Diagnostics.Process[] ProcessID;


        public ProcessMemory(String ProcessName)
        {
            GetProcessHandle(ProcessName);
        }

        //Get Process handle
        private void GetProcessHandle(String ProcessName)
        {
            this.ProcessID = System.Diagnostics.Process.GetProcessesByName(ProcessName);
            if (this.ProcessID.Length > 0)
            {
                this.ProcessHandle = RefWinApi.OpenProcess(RefWinApi.PROCESS_ALL_ACCESS, 0, (UInt32)ProcessID[0].Id);
            }
            else
            {
                MessageBox.Show("Get ProcessID fail!");
            }
        }

        //Read memory data
        public bool Read(IntPtr StartAddress, uint[] DataBuf, UInt32 length)
        {
            bool isRead = false;
            Int32 s = RefWinApi.ReadProcessMemory(this.ProcessHandle, StartAddress, DataBuf, length, (IntPtr)0);
            if (s != 0)
            {
                isRead = true;
            }
            return isRead;
        }

        //Write data to memory
        public bool Write(IntPtr StartAddress, uint[] Data, UInt32 length)
        {
            bool isWrite = false;
            Int32 s = RefWinApi.WriteProcessMemory(this.ProcessHandle, StartAddress, Data, length, (IntPtr)0);
            if (s != 0)
            {
                isWrite = true;
            }
            return isWrite;
        }

        //Close Handle
        public void Close()
        {
            RefWinApi.CloseHandle(this.ProcessHandle);
        }
    }


    //class RefWinApi
    //import WinApi
    public class RefWinApi
    {
        //Mesagebox
        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        public static extern int MessageBox(IntPtr hWnd, String text, String caption, uint type);

        //HANDLE OpenProcess(
        // DWORD dwDesiredAccess,// access flag
        // BOOL bInheritHandle, // handle inheritance flag
        // DWORD dwProcessId // process identifier
        // );
        [DllImport("kernel32.dll")]
        public static extern IntPtr OpenProcess(UInt32 dwDesiredAccess, Int32 bInheritHandle, UInt32 dwProcessId);

        //BOOL CloseHandle(
        // HANDLE hObject // handle to object to close
        // );
        [DllImport("kernel32.dll")]
        public static extern Int32 CloseHandle(IntPtr hObject);

        //BOOL WriteProcessMemory(
        // HANDLE hProcess, // handle to process whose memory is written to
        // LPVOID lpBaseAddress, // address to start writing to
        // LPVOID lpBuffer, // pointer to buffer to write data to
        // DWORD nSize, // number of bytes to write
        // LPDWORD lpNumberOfBytesWritten // actual number of bytes written
        // );
        [DllImport("kernel32.dll")]
        public static extern Int32 WriteProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, uint[] lpBuffer, UInt32 nSize, IntPtr lpNumberOfBytesWritten);

        //BOOL ReadProcessMemory(
        // HANDLE hProcess, // handle of the process whose memory is read
        // LPCVOID lpBaseAddress, // address to start reading
        // LPVOID lpBuffer, // address of buffer to place read data
        // DWORD nSize, // number of bytes to read
        // LPDWORD lpNumberOfBytesRead // address of number of bytes read
        // );
        [DllImport("kernel32.dll")]
        public static extern Int32 ReadProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, uint[] lpBuffer, UInt32 nSize, IntPtr lpNumberOfBytesRead);

        //member

        //
        public const uint PROCESS_ALL_ACCESS = 0x001F0FFF;


        //
    }//end of class RefWinApi
}

